package com.stylefeng.guns.modular.vpn_real_time_data.service.impl;

import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.model.VpnRealTimeData;
import com.stylefeng.guns.modular.system.dao.VpnRealTimeDataMapper;
import com.stylefeng.guns.modular.vpn.entity.RealTimeDetail;
import com.stylefeng.guns.modular.vpn.entity.RealTimeTodayInfo;
import com.stylefeng.guns.modular.vpn.entity.RealmTimeSeries;
import com.stylefeng.guns.modular.vpn.utils.NumberUtil;
import com.stylefeng.guns.modular.vpn.utils.RealmTimeUtil;
import com.stylefeng.guns.modular.vpn_real_time_data.service.IVpnRealTimeDataService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-03-11
 */
@Service
public class VpnRealTimeDataServiceImpl extends ServiceImpl<VpnRealTimeDataMapper, VpnRealTimeData> implements IVpnRealTimeDataService {
    @Autowired
    private VpnRealTimeDataMapper realTimeDataMapper;

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public RealTimeTodayInfo getTodayRealTime() {
        RealTimeTodayInfo realTimeTodayInfo = realTimeDataMapper.findTodayRealTimeInfo();
        BigDecimal todayRecordOnlineTime = realTimeDataMapper.queryTodayOnlineTime();
        BigDecimal yesterdayOnlineTime = realTimeDataMapper.queryYesterdayOnlineTime();

        realTimeTodayInfo.setTodayOnlineTime(String.valueOf(todayRecordOnlineTime == null?0:NumberUtil.formatDouble(todayRecordOnlineTime.doubleValue(),"#.0000")));
        realTimeTodayInfo.setYesterdayOnlineTime(yesterdayOnlineTime == null?"0":String.valueOf(yesterdayOnlineTime.doubleValue()));

        return realTimeTodayInfo;
    }

    /**
     * 指标图实时数据查询
     * @param type 查询数据类型，1-注册用户数，2-上线用户数，
     *             3-在线时长，4-新增付费用户数，5-新增付费率
     * @return
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public List<RealmTimeSeries> echartsRealTimeInfo(String type) {

        List<Map<String,Object>> echartsForms = null;
        if(type.equals("1")){
            echartsForms = realTimeDataMapper.regCountInfo();
        }
        else if(type.equals("2")){
            echartsForms = realTimeDataMapper.onlineCountInfo();
        }
        else if(type.equals("3")){
            echartsForms = realTimeDataMapper.onlineTimeInfo();
        }
        else if(type.equals("4")){
            echartsForms = realTimeDataMapper.paymentCountInfo();
        }
        else if(type.equals("5")){
            echartsForms =
                    RealmTimeUtil.payRatioChange(realTimeDataMapper.paymentRatioInfo());
        }
        return RealmTimeUtil.getRealTimeList(Integer.parseInt(type),echartsForms);
    }

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public List<RealTimeDetail> getDetailOfTodayRealTime() {
        List<Map<String, Object>> regDetail  = realTimeDataMapper.queryRegDetail();
        List<Map<String, Object>> connectDetail  = realTimeDataMapper.queryConnectDetail();
        List<Map<String, Object>> paymentDetail  = realTimeDataMapper.queryPaymentDetail();

        List<Map<String, Object>> maxList = new ArrayList<>();
        Map<String, RealTimeDetail>  realTimeDetailMap = new HashMap<>();

        List<RealTimeDetail> detailList = new ArrayList<>();

        if(regDetail != null){
            maxList.addAll(regDetail);
        }
        if(connectDetail != null){
            maxList.addAll(connectDetail);
        }

        if(paymentDetail != null) {
            maxList.addAll(paymentDetail);
        }

        for (Map<String,Object> map:maxList){
            String  hTimeKey = (String) map.get("hTime");
            RealTimeDetail realTimeDetail = realTimeDetailMap.get(hTimeKey);
            if(realTimeDetail == null){
                realTimeDetail = new RealTimeDetail();
                realTimeDetail.setHTime(hTimeKey);
            }
            if(realTimeDetail.getRegCount() == null){
                realTimeDetail.setRegCount(map.get("regCount"));
            }
            if(realTimeDetail.getTotalRegCount() == null){
                realTimeDetail.setTotalRegCount(map.get("totalCount"));
            }
            if(realTimeDetail.getOnlineCount() == null){
                realTimeDetail.setOnlineCount(map.get("onlineCount"));
            }
            if(realTimeDetail.getTotalOnlineTime() == null){
                realTimeDetail.setTotalOnlineTime (map.get("totalOnlineTime"));
            }
            if(realTimeDetail.getPayCount() == null){
                realTimeDetail.setPayCount (map.get("payCount"));
            }

            realTimeDetailMap.put(hTimeKey,realTimeDetail);
        }

        for (Map.Entry<String,RealTimeDetail> entry:realTimeDetailMap.entrySet()){
            detailList.add(entry.getValue());
        }

        Collections.sort(detailList);
        int size = detailList.size();
        for (int i = size-2; i >=0 ; i--) {
            if (detailList.get(i).getTotalRegCount() == null){
                detailList.get(i).setTotalRegCount(detailList.get(i+1).getTotalRegCount());
            }
        }

        return detailList;
    }

}
